<template>
	<xn-form-container
		:title="formData.scientificName ? '编辑鱼种类' : '增加鱼种类'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="命名者：" name="namedByMan">
						<a-input v-model:value="formData.namedByMan" placeholder="请输入命名者" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="中文名：" name="chineseName">
						<a-input v-model:value="formData.chineseName" placeholder="请输入中文名" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="科中文名：" name="branchChineseName">
						<a-input v-model:value="formData.branchChineseName" placeholder="请输入科中文名" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="科字科名：" name="sectionName">
						<a-input v-model:value="formData.sectionName" placeholder="请输入科字科名" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="最大体长（cm)：" name="maxBodyLength">
						<a-input v-model:value="formData.maxBodyLength" placeholder="请输入最大体长（cm)" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="世界分布：" name="worldDistribution">
						<a-input v-model:value="formData.worldDistribution" placeholder="请输入世界分布" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="栖息环境：" name="habitat">
						<a-input v-model:value="formData.habitat" placeholder="请输入栖息环境" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="同种异名：" name="synonyms">
						<a-input v-model:value="formData.synonyms" placeholder="请输入同种异名" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="参考文献：" name="cankaowenxian">
						<a-input v-model:value="formData.cankaowenxian" placeholder="请输入参考文献" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="典藏标本：" name="collectedSpecimens">
						<a-input v-model:value="formData.collectedSpecimens" placeholder="请输入典藏标本" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="英文俗名：" name="englishCommonName">
						<a-input v-model:value="formData.englishCommonName" placeholder="请输入英文俗名" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="濒危状态(是否在IUCN濒危名单中）1：是  0：否：" name="endangeredStatus">
						<a-input v-model:value="formData.endangeredStatus" placeholder="请输入濒危状态(是否在IUCN濒危名单中）1：是  0：否" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="形态特征：" name="morphologicalCharacteristics">
						<a-input v-model:value="formData.morphologicalCharacteristics" placeholder="请输入形态特征" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="栖所生态：" name="habitatEcology">
						<a-input v-model:value="formData.habitatEcology" placeholder="请输入栖所生态" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="地理分布：" name="geographicalDistribution">
						<a-input v-model:value="formData.geographicalDistribution" placeholder="请输入地理分布" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="渔业利用：" name="fisheryUtilization">
						<a-input v-model:value="formData.fisheryUtilization" placeholder="请输入渔业利用" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="栖息深度：" name="habitatDepth">
						<a-input v-model:value="formData.habitatDepth" placeholder="请输入栖息深度" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="有毒鱼 （1：是 0：否）：" name="toxicFish">
						<a-input v-model:value="formData.toxicFish" placeholder="请输入有毒鱼 （1：是 0：否）" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="经济鱼 （1：是 0：否）：" name="economicFish">
						<a-input v-model:value="formData.economicFish" placeholder="请输入经济鱼 （1：是 0：否）" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="食用鱼（1：是 0：否）：" name="edibleFish">
						<a-input v-model:value="formData.edibleFish" placeholder="请输入食用鱼（1：是 0：否）" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="观赏鱼 （1：是 0：否）：" name="ornamentalFish">
						<a-input v-model:value="formData.ornamentalFish" placeholder="请输入观赏鱼 （1：是 0：否）" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="图片：" name="image">
						<a-input v-model:value="formData.image" placeholder="请输入图片" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="其他中文名：" name="otherChineseName">
						<a-input v-model:value="formData.otherChineseName" placeholder="请输入其他中文名" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="fishDataForm">
import { cloneDeep } from 'lodash-es'
import { required } from '@/utils/formRules'
import fishDataApi from '@/api/biz/fishDataApi'
// 抽屉状态
const visible = ref(false)
const emit = defineEmits({ successful: null })
const formRef = ref()
// 表单数据
const formData = ref({})
const submitLoading = ref(false)

// 打开抽屉
const onOpen = (record) => {
	visible.value = true
	if (record) {
		let recordData = cloneDeep(record)
		formData.value = Object.assign({}, recordData)
	}
}
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields()
	formData.value = {}
	visible.value = false
}
// 默认要校验的
const formRules = {
}
// 验证并提交数据
const onSubmit = () => {
	formRef.value.validate().then(() => {
		submitLoading.value = true
		const formDataParam = cloneDeep(formData.value)
		fishDataApi
			.fishDataSubmitForm(formDataParam, formDataParam.scientificName)
			.then(() => {
				onClose()
				emit('successful')
			})
			.finally(() => {
				submitLoading.value = false
			})
	})
}
// 抛出函数
defineExpose({
	onOpen
})
</script>
